#include "DB.h"
#include <fstream>
#include <string.h>
#include <stdint.h>
#include <iostream>

using namespace std;


fstream outFile("data.hex",  ios::out | ios::binary);

class F_DB: public SD_DB
{
private:	
    void DB_write_byte(uint32_t address, byte data);
    byte DB_read_byte(uint32_t address);
};

void F_DB::DB_write_byte(uint32_t address, byte data)
{
	char buffer[1];
	buffer[0]=data;
	outFile.seekp (address);
	outFile.write (buffer,sizeof(buffer));
}

byte F_DB::DB_read_byte(uint32_t address)
{
	char buffer[1];
	outFile.seekg (address);
	outFile.read(buffer,sizeof(buffer));
	return buffer[0];
}

F_DB db;



void processOBJECTS(struct master m)
{
        int i=0,j=0;
	char c;
	objects ngc0;
	string line;
	c=m.name[j];
	while (c != 0x0){
	        line.push_back(c);
		j++;	
		c=m.name[j];
	}
	line.append(".ixd");
        cout << "recordsize: "<< sizeof(ngc0) << endl;
        cout << "file name: "<< line << endl;
        cout << "records: "<< m.numRecords << endl;
        fstream inFile(line.c_str(),  ios::in | ios::binary);
       	inFile.read((char*) &ngc0,sizeof(ngc0));
 	while (!inFile.eof()) {
		i++;
          	inFile.read((char*) &ngc0,sizeof(ngc0));
	} 
        inFile.close();
        inFile.open(line.c_str(),  ios::in | ios::binary);
  	db.DBcreate(m.offset, i*sizeof(ngc0)+12, sizeof(ngc0));
       	inFile.read((char*) &ngc0,sizeof(ngc0));
	while (!inFile.eof()) {

		cout << "========="<< ngc0.tile<<"==========" << endl;
	        cout << "TILE: "<< ngc0.tile << endl;
	        cout << "STARS: "<< ngc0.stars_from <<"  "<< ngc0.stars_to << endl;
	        cout << "NGC: "<< ngc0.ngc_from << "  "<< ngc0.ngc_to << endl;
	        cout << "MESSIER: "<< ngc0.messier_from << "  "<< ngc0.messier_to << endl;

	db.DBappendRec(DB_REC ngc0);

       	inFile.read((char*) &ngc0,sizeof( ngc0));
 	} 
        inFile.close(); 
}


void processSTARS(struct master m)
{
        int i=0,j=0;
	char c;
	star ngc0;
	string line;
	c=m.name[j];
	while (c != 0x0){
	        line.push_back(c);
		j++;	
		c=m.name[j];
	}
	line.append(".ixd");
        cout << "recordsize: "<< sizeof(ngc0) << endl;
        cout << "file name: "<< line << endl;
        cout << "records: "<< m.numRecords << endl;
        fstream inFile(line.c_str(),  ios::in | ios::binary);
       	inFile.read((char*) &ngc0,sizeof(ngc0));
 	while (!inFile.eof()) {
		i++;
          	inFile.read((char*) &ngc0,sizeof(ngc0));
	} 
        inFile.close();
        inFile.open(line.c_str(),  ios::in | ios::binary);
  	db.DBcreate(m.offset, i*sizeof(ngc0)+12, sizeof(ngc0));
       	inFile.read((char*) &ngc0,sizeof(ngc0));
	while (!inFile.eof()) {

		cout << "========="<< ngc0.HIP<<"==========" << endl;
	        cout << "HIP:"<< ngc0.HIP << endl;
	        cout << "RA:"<< ngc0.ra << endl;
	        cout << "DE:"<< ngc0.dec << endl;
	        cout << "MAG:"<< ngc0.mag << endl;
	        cout << "F:"<< ngc0.F << endl;
	        cout << "BAYER:"<< ngc0.bayer << endl;
	        cout << "COS:"<< ngc0.costellation << endl;
	        cout << "NAME:"<< ngc0.name << endl;

	db.DBappendRec(DB_REC ngc0);

       	inFile.read((char*) &ngc0,sizeof( ngc0));
 	} 
        inFile.close(); 
}


void processNGC(struct master m)
{
        int i=0,j=0;
	char c;
	ngc ngc0;
	string line;
	c=m.name[j];
	while (c != 0x0){
	        line.push_back(c);
		j++;	
		c=m.name[j];
	}
	line.append(".ixd");
        cout << "recordsize: "<< sizeof(ngc) << endl;
        cout << "file name: "<< line << endl;
        cout << "records: "<< m.numRecords << endl;
        fstream inFile(line.c_str(),  ios::in | ios::binary);
       	inFile.read((char*) &ngc0,sizeof(struct ngc));
 	while (!inFile.eof()) {
		i++;
          	inFile.read((char*) &ngc0,sizeof(struct ngc));
	} 
        inFile.close();
        inFile.open(line.c_str(),  ios::in | ios::binary);
  	db.DBcreate(m.offset, i*sizeof(ngc0)+12, sizeof(ngc0));
       	inFile.read((char*) &ngc0,sizeof(struct ngc));
	while (!inFile.eof()) {

		cout << "========="<< ngc0.ngc_n<<"==========" << endl;
	        cout << "NGC:"<< ngc0.ngc_n << endl;
	        cout << "M/NGC/IC:"<< ngc0.ngc_ic_messier << endl;
	        cout << "RA:"<< ngc0.ra << endl;
	        cout << "DE:"<< ngc0.dec << endl;
	        cout << "MAG:"<< ngc0.mag << endl;
	        cout << "IMAGE:"<< ngc0.image << endl;
	        cout << "NAME:"<< ngc0.name << endl;
	        cout << "TYPE:"<< ngc0.type << endl;
	        cout << "TILE:"<< ngc0.tile << endl;

	db.DBappendRec(DB_REC ngc0);

       	inFile.read((char*) &ngc0,sizeof(struct ngc));
 	} 
        inFile.close(); 
}


main() {
        int i=0,j=0;
	master master0;
	cout << "master "<< sizeof(master) << endl;
        fstream inFile("master.ixd",  ios::in | ios::binary);
       	inFile.read((char*) &master0,sizeof(struct master));
 	while (!inFile.eof()) {
		i++;
          	inFile.read((char*) &master0,sizeof(struct master));
	} 
	int masterSize=i;
        inFile.close();
	master m_record[masterSize];
  	db.DBcreate(0, masterSize*sizeof(master0)+12, sizeof(master0));
	i=0;
        inFile.open("master.ixd",  ios::in | ios::binary);
        cout << "========= MASTER RECORD ============" << endl;
	cout << "RECORDS:"<< i << endl;
	cout << "RECORD SIZE:"<< sizeof(master0) << endl;
       	inFile.read((char*) &master0,sizeof(struct master));
	while (!inFile.eof()) {
	cout << "========="<< master0.name<<"==========" << endl;
	cout << "OFFSET:"<< (unsigned int)master0.offset << endl;
	cout << "TOTALSIZE:"<< (unsigned int) master0.totalSize << endl;
	cout << "RECSIZE:"<< (unsigned int) master0.recordSize << endl;
	cout << "NumRecords:"<< (unsigned int) master0.numRecords << endl;
	cout << "AuxValue:"<< (unsigned short)master0.auxValue << endl;
	m_record[i]=master0;
	db.DBappendRec(DB_REC master0);
	cout << master0.name <<" MASTER RECORD INSERTED" << endl;
	i++;
       	inFile.read((char*) &master0,sizeof(struct master));
 	} 
        inFile.close();

	for (j=0;j<masterSize;j++) {
	master0=m_record[j];
	cout << "========="<< master0.name<<"==========" << endl;
	cout << "OFFSET:"<< (unsigned int)master0.offset << endl;
	cout << "TOTALSIZE:"<< (unsigned int) master0.totalSize << endl;
	cout << "RECSIZE:"<< (unsigned int) master0.recordSize << endl;
	cout << "NumRecords:"<< (unsigned int) master0.numRecords << endl;
	cout << "AuxValue:"<< (unsigned short)master0.auxValue << endl;
	switch(j) {
		case NGC:
			cout << "NGC:" << endl;
			processNGC(master0);
			break;
		case MESSIER:
			cout << "MESSIER:" << endl;
			processNGC(master0);
			break;
		case NAMED_STARS:
			cout << "STARS:" << endl;
			processSTARS(master0);
			break;
		case OBJECTS_IN_TILE:
			cout << "STARS:" << endl;
			processOBJECTS(master0);
			break;
		default:
			break;
        }
 	} 


        inFile.close(); 
	outFile.close();
}
